<?php
namespace app\admin\controller;

class Admin extends Common
{

    private $obj;

    public function _initialize()
    {
        parent::_initialize();
        $this->obj = model('Admin');
    }

    public function index()
    {
        /**
         * 获取管理员列表
         */
        $data = $this->obj->getAdminList();
        $this->assign('data', $data);
        return view();
    }

    public function save_admin() {
        $id = input('param.id', 0);
        if (request()->isPost()) {
            /**
             * 组装数据
             */
            $data['username'] = input('post.username', '');
            $data['password'] = input('post.password', '');
            $data['email'] = input('post.email', '');
            $data['mobile'] = input('post.mobile', '');
            $data['repassword'] = input('post.repassword', '');

            /**
             * 验证数据
             */
            $validate = validate('Admin');
            if($id == 0) {
                if(!$validate->check($data)) {
                    $this->error($validate->getError());
                }
            } else {
                if ($data['password'] && $data['repassword']) {
                    if(!$validate->check($data)) {
                        $this->error($validate->getError());
                    }
                } else {
                    unset($data['password']);
                    $validate->scene('edit', ['username', 'email', 'mobile']);
                    if(!$validate->scene('edit')->check($data)) {
                        $this->error($validate->getError());
                    }
                }
            }
            unset($data['repassword']);

            /**
             * 检查用户名是否存在
             */
            $where['id'] = $id > 0 ? ['neq', $id] : ['neq', 0];
            $where['username'] = $data['username'];
            $admin = $this->obj->getAdminByUsername($where);
            if ($admin) {
                $this->error('用户名已存在');
            }

            /**
             * 加密密碼
             */
            if (isset($data['password'])) {
                $rand_char = getRandChar(6);
                $data['password'] = md5($rand_char . $data['password']);
                $data['rand_char'] = $rand_char;
            }

            /**
             * 保存数据
             */
            $where = $id > 0 ? ['id' => $id] : [];
            $rs = $this->obj->save($data, $where);
            if ($rs) {
                /**
                 * 添加权限
                 */
                $admin_role = input('param.admin_role', 0);
                $role_data['jurisdiction_group_id'] = $admin_role;
                if ($id > 0) {
                    model('AdminJurisdiction')->save($role_data, ['admin_id' => $id]);
                } else {
                    $id = $this->obj->id;
                    $role_data['admin_id'] = $id;
                    model('AdminJurisdiction')->save($role_data);
                }
                // 清理缓存
                \app\api\service\Base::clear_cache();
                
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        } else{
            $role_list = model('JurisdictionGroup')->getRoleList(['status' => 1]);
            $this->assign('role_list', $role_list);

            $data = [];
            if ($id > 0) {
                $data = $this->obj->get(['id' => $id]);
            }
            $this->assign('data', $data);

            return view();
        }
    }

}
